package com.tms.app.tms.ui.home.msg;


import android.content.Intent;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tms.app.tms.adapter.OrderChildAdapter;
import com.tms.app.tms.app.Constant;
import com.tms.app.tms.app.MyApp;
import com.tms.app.tms.bean.Msg;
import com.tms.app.tms.bean.TestBean;
import com.tms.app.tms.bean.response.BaseResponse;
import com.tms.app.tms.bean.response.MsgRes;
import com.tms.app.tms.db.MsgDBUtil;
import com.tms.app.tms.ui.base.view.IListView;
import com.tms.app.tms.ui.base.presenter.AbsBaseListP;
import com.tms.app.tms.ui.taskinfo.TaskInfoActivity;

import org.xutils.http.RequestParams;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by happy on 2017/8/28.
 */

public class OrderChildP extends AbsBaseListP {
    IListView iViewTrade;
    private String token;
    private long time=0;

    public void setiView(IListView iViewTrade) {
        this.iViewTrade = iViewTrade;
    }

    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        BaseResponse baseResponse=gson.fromJson(result,BaseResponse.class);
        MsgRes msgRes=gson.fromJson(result,MsgRes.class);
        sendMsgToView(msgRes);
        bindDataToView(msgRes);
    }


    private void bindDataToView(MsgRes msgRes){

        if (iViewTrade==null)
            return;
        if (msgRes==null||msgRes.getCode()!=200||msgRes.getData().getList().size()<1){
            iViewTrade.refreshNoData();
            return;
        }
        MsgDBUtil.saveOrUpadateMsg(msgRes.getData().getList(),0);
        OrderChildAdapter depthAdapter= iViewTrade.getAdapter(0);
        depthAdapter.getData().clear();
//        depthAdapter.getData().addAll(MsgDBUtil.getMsgListByType(0));
        depthAdapter.getData().addAll(msgRes.getData().getList());
        depthAdapter.notifyDataSetChanged();
        iViewTrade.refreshComplite("");
    }




    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            Msg msg= (Msg) adapter.getData().get(position);
            Intent intent=new Intent(view.getContext(),TaskInfoActivity.class);
            intent.putExtra("planId",msg.getPlanId());
            intent.putExtra("readStatus",1);
            intent.putExtra("type",3);
            iViewTrade.startActivity(intent);
    }


    public void getData(String token, int pageIndex){
        this.token=token;
        OrderChildAdapter depthAdapter= iViewTrade.getAdapter(0);
        if (depthAdapter.getData()!=null&&depthAdapter.getData().size()>0){
            Msg msg=depthAdapter.getData().get(depthAdapter.getData().size()-1);
            time=msg.getPublishTime();
        }
        RequestParams params=new RequestParams(Constant.TaskMsg);
        params.addBodyParameter("token",token);
        params.addBodyParameter("time",time+"");
        params.addBodyParameter("pageSize","200");//
        params.addBodyParameter("pageIndex",pageIndex+"");
        getObjectFromServer(params);

    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        getData(token,1);
    }
}
